package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.TimeCount;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqSmsCode;
import com.lanlian.commonlib.bean.ReqVerifySmsCode;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.utillib.CommonUtils;
import com.lanlian.utillib.WorksSizeCheckUtil;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/7 11:38
 *     desc   : 验证手机号码页面
 *     version: 1.0
 * </pre>
 */
public class VerifyMobileActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtMobile;
    private TextView mTvCode;
    private EditText mEtCode;
    private TextView mTvOperate;

    private TimeCount mTimeCount;
    private int mOperateId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_mobile, true);
        initData();
        initView();
        setListeners();
        mTimeCount = new TimeCount(this, 60000, 1000, mTvCode);
    }

    @Override
    protected void initData() {
        super.initData();
        mOperateId = getIntent().getIntExtra("operateId", 0);
    }

    @Override
    protected void initView() {
        super.initView();
        mEtMobile = findViewById(R.id.et_mobile);
        mTvCode = findViewById(R.id.tv_code);
        mEtCode = findViewById(R.id.et_code);
        mTvOperate = findViewById(R.id.tv_operate);
        switch (mOperateId) {
            case 1:
                setTitle("开户进度查询");
                mTvOperate.setText("查询");
                break;
            case 2:
                setTitle("投资风险调研");
                mTvOperate.setText("开始调研");
                break;
            case 3:
                setTitle("上传资料");
                mTvOperate.setText("上传资料");
                break;
            case 4:
                setTitle("好友认证");
                mTvOperate.setText("生成认证链接");
                break;
            default:
                break;
        }
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvCode.setOnClickListener(this);
        mTvOperate.setOnClickListener(this);
        mImgBack.setOnClickListener(this);
        WorksSizeCheckUtil worksSizeCheckUtil = new WorksSizeCheckUtil(mTvOperate);
        worksSizeCheckUtil.addAllEditText(mEtCode, mEtMobile);
        worksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    mTvOperate.setEnabled(true);
                    mTvOperate.setBackgroundResource(R.mipmap.login_btn);
                } else {
                    mTvOperate.setEnabled(false);
                    mTvOperate.setBackgroundResource(R.drawable.bg_click_false);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_operate:
                if (!CommonUtils.isMobile(mEtMobile.getText().toString())) {
                    showToast("请输入正确的手机号码");
                    return;
                }
                ReqVerifySmsCode reqVerifySmsCode = new ReqVerifySmsCode();
                reqVerifySmsCode.setCode(mEtCode.getText().toString());
                reqVerifySmsCode.setMobile(mEtMobile.getText().toString());
                showLoadingDialog();
                VolleyRequestUtils.getInstance().verifySmsCode(this.toString(), reqVerifySmsCode, new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        dismissLoadingDialog();
                        jump(mEtMobile.getText().toString());
                        finish();
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        dismissLoadingDialog();
                        showToast(baseResponse.getMsg());
                    }
                });
                break;
            case R.id.tv_code:
                if (!CommonUtils.isMobile(mEtMobile.getText().toString())) {
                    showToast("请输入正确的手机号码");
                    return;
                }
                mTvCode.setClickable(false);
                VolleyRequestUtils.getInstance().sendSmsCode(this.toString(),
                        new ReqSmsCode(mEtMobile.getText().toString()), new INetWorkCallback() {
                            @Override
                            public void onSuccess(Object response) {
                                mTimeCount.startTime();
                                showToast("验证码已发送到你的手机");
                            }

                            @Override
                            public void onFail(BaseResponse baseResponse) {
                                mTvCode.setClickable(true);
                                showToast(baseResponse.getMsg());
                            }
                        });
                break;
            default:
                break;
        }
    }

    private void jump(String mobile) {
        switch (mOperateId) {
            case 1:
                startActivity(new Intent(this, RegisterStatusActivity.class)
                        .putExtra("mobile", mobile));
                break;
            case 2:
                startActivity(new Intent(this, RiskStateActivity.class)
                        .putExtra("mobile", mobile));
                break;
            case 3:
                startActivity(new Intent(this, UploadActivity.class)
                        .putExtra("mobile", mobile));
                break;
            case 4:
                startActivity(new Intent(this, FriendAuthenticationActivity.class)
                        .putExtra("mobile", mobile));
                break;
            default:
                break;
        }
    }

}
